//
//  MainViewController.m
//  时光电影项目
//
//  Created by 墨狂之逸才 on 15/7/17.
//  Copyright (c) 2015年 墨狂之逸才. All rights reserved.
//

#import "MainViewController.h"
#import "MovieModal.h"
#import "MovieCell.h"


@interface MainViewController ()
{
    BOOL isLeft;
    NSMutableArray *_movieModalArray;
}
@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
     [self _creatNav];
    
     [self _creatMoviceTableView];
    
    [self _creatPosterView];
    
   

    
    [self _loadData];
//    NSArray *nameArray = [NSArray array];
//    nameArray = @[@"首页",@"新闻",@"Top250",@"影院",@"更多"];
//    
//    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
//    label.text = @"212121";
//    label.textColor = [UIColor whiteColor];
//    label.textAlignment = NSTextAlignmentCenter;
//    [self.navigationController.navigationBar addSubview:label];

    
 
    
    
//      self.view.backgroundColor = [UIColor orangeColor];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//ios 7 以下使用如下方法修改状态栏的样式，多层关系种，nav的子控件里面重写该方法无效
- (UIStatusBarStyle)preferredStatusBarStyle
{
    NSLog(@"preferredStatusBarStyle");
    //黑色
    //    return UIStatusBarStyleDefault;
    
    //白色
    return UIStatusBarStyleLightContent;
    
}

#pragma mark - 导航栏右侧翻页按钮设置
- (void)_creatNav
{
    
    UIView *flipView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn1 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home@2x"] forState:UIControlStateNormal];
    [btn1 setImage:[UIImage imageNamed:@"poster_home"] forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(flipAction:) forControlEvents:UIControlEventTouchUpInside];
    [btn1 sizeToFit];
    btn1.tag = 1;
    [flipView addSubview:btn1];
     btn1.hidden = NO;
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn2 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home@2x"] forState:UIControlStateNormal];
    [btn2 setImage:[UIImage imageNamed:@"list_home"] forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(flipAction:) forControlEvents:UIControlEventTouchUpInside];
    [btn2 sizeToFit];
    btn2.tag = 2;
    [flipView addSubview:btn2];
    btn2.hidden = YES;
    
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:flipView];
    self.navigationItem.rightBarButtonItem = item;
    
    //设置导航栏为半透明，以便使tableView的内容在延展的情况下，自动调整内容。
    self.navigationController.navigationBar.translucent = YES;
}

#pragma mark - 翻页效果
- (void)flipAction:(UIButton *)button
{
    UIView *flipView = self.navigationItem.rightBarButtonItem.customView;
    UIButton *button1 = (UIButton *)[flipView viewWithTag:1];
    UIButton *button2 = (UIButton *)[flipView viewWithTag:2];
    
    UIViewAnimationOptions option = isLeft?UIViewAnimationOptionTransitionFlipFromLeft:UIViewAnimationOptionTransitionFlipFromRight;
    
    isLeft = !isLeft;
    
    [UIView transitionWithView:flipView duration:0.5 options:option animations:nil completion:nil];
    
    [UIView transitionWithView:self.view duration:0.5 options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil completion:nil];
    
    
    button1.hidden = !button1.hidden;
    button2.hidden = !button2.hidden;
    
    
    _moviceTableView.hidden = !_moviceTableView.hidden;
    _posterView.hidden = ! _posterView.hidden;
  
}

//- (void)setImageUrlDic:(NSDictionary *)imageUrlDic
//{
//    _imageUrlDic = imageUrlDic;
//    
//}

#pragma mark - 海报页面
- (void)_creatPosterView
{
    _posterView = [[PosterView alloc]initWithFrame:self.view.bounds];
    _posterView.backgroundColor = [UIColor orangeColor];
    
//    _posterView.imageUrlArray = _modal.images;
    
    [self.view addSubview:_posterView];
    
}

#pragma mark - 电影列表页面
- (void)_creatMoviceTableView
{
    _moviceTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _moviceTableView.delegate = self;
    _moviceTableView.dataSource = self;
    _moviceTableView.hidden = YES;
    
    [self.view addSubview:_moviceTableView];
    
    //注册nib
    UINib *nib = [UINib nibWithNibName:@"MovieCell" bundle:[NSBundle mainBundle]];
    //让tableView来注册nib
    [_moviceTableView registerNib:nib forCellReuseIdentifier:@"cell"];
    
}


#pragma mark - 返回row的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _movieModalArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //cell不会为nil，会自动创建
    MovieCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    //如果在这里设置cell的选中属性，就是没创建一个就会来一次设置，效率很低，不推荐
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
//    cell.textLabel.text = @"ss";
    
    
    //这样写是错误的，因为数组里面取出来的是id，类型，没有text的属性，要先赋值给创建出来的movieModal
//    cell.textLabel.text = _movieModalArray[indexPath.row];
   
    
    //获取对应一行的的cell要显示的内容
    MovieModal *movieModal = _movieModalArray[indexPath.row];
    
    //将movieModal对对应内容赋给cell，非定制cell的情况下，直接将模型数据赋给cell
//    cell.textLabel.text = movieModal.title;
    
    //因为具体的cell多内容不在这里设置，而在其子类MovieCell里面做具体的设置，将cell数据源的数据传递到movieCell里面的movieMdal属性里面去，
    //要自定义自己的cell，以方便实现自己想要的风格
    
    
    //cell里面的modal就等于我＝创建cell的时候我拿到的modal
    //这句代码，是一个set方法，会调用set方法，和awakeFromNib来创建cell
    cell.movieModal = movieModal;
    static int i= 0;
    i++;
    NSLog(@"%d",i);
    return cell;
}

#pragma mark - downLoad下载数据
- (void)_loadData
{
    
    _movieModalArray = [[NSMutableArray alloc]init];
    NSString *paths = [[NSBundle mainBundle]pathForResource:@"us_box.json" ofType:nil];
    
    
    //NSData直接存储二进制数据的
    NSData *data = [NSData dataWithContentsOfFile:paths];
    
    //解析json数据,将json数据存入字典或数组中
    NSDictionary *movieDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSArray *subjects = [movieDic objectForKey:@"subjects"];
    
    for (NSDictionary *dic in subjects) {
        MovieModal *movieModal = [[MovieModal alloc]init];
        NSDictionary *subject = [dic objectForKey:@"subject"];
        movieModal.title = [subject objectForKey:@"title"];
        movieModal.year = [subject objectForKey:@"year"];
        movieModal.images = [subject objectForKey:@"images"];
        
        float average = [[[subject objectForKey:@"rating"]objectForKey:@"average"]  floatValue];
        movieModal.average = average;
        
        

        
        
        [_movieModalArray addObject:movieModal];
    }
    
        _posterView.movieModalArray = _movieModalArray;
    
}

//- (void)setImageUrlArray:(NSArray *)imageUrlArray
//{
//    _imageUrlArray = imageUrlArray;
//    
//    _posterView.imageUrlArray = [_modal.images objectForKey:@"large"];
//    
//    
//
//}


#pragma mark - 返回cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
